

package bookings;

import java.util.HashMap;
import java.util.HashSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

public class CartProcess {
    
    private String id;  
    private String campDate;  
    
    public CartProcess() {
    }
    
    public String Add() throws Exception {        
        
        String result="success";
        
        HttpServletRequest request=ServletActionContext.getRequest();  
        HttpSession session=request.getSession();
        
        HashSet<bookings.CartItem> cart=null;
        //取出購物車
        if(session.getAttribute("Cart")==null)
            cart=new HashSet<bookings.CartItem>();
        else
            cart=(HashSet<bookings.CartItem>)session.getAttribute("Cart");
        
        bookings.CartItem ci=new bookings.CartItem();
        ci.setId(id);
        ci.setCampDate(campDate);
//        String date = request.getParameter("campDate");
        if(!cart.contains(ci))
        {
             //由資料庫中讀出該項商品的資訊
            db.NWDB mydb=new db.NWDB();
            HashMap<String,Object> hm=mydb.getCampInfo(id,campDate);

            ci.setName(hm.get("營地名稱").toString());
            ci.setCampsize(hm.get("營地大小").toString());
            ci.setNprice((Integer)hm.get("營地定價"));
            ci.setHoliday(hm.get("假別").toString());
            ci.setDiscounts(hm.get("折扣數").toString());
            ci.setPrice((Integer)hm.get("價格"));
            
            //            HashMap<String,Object> hm=mydb.getCampInfo(id);
            //            ci.setName(hm.get("營地名稱").toString());
            //            ci.setCampsize(hm.get("營地大小").toString());
            //            ci.setNprice((Integer)hm.get("營地定價"));
            //            ci.setHoliday(hm.get("假別").toString());
            cart.add(ci);
            //儲存回用戶的Session
            session.setAttribute("Cart", cart);
//            session.setAttribute("CheckinDate",campDate);
        }
        return result;        
    }
  
    public String Clear() throws Exception {        
        
        String result="clear";
        
        HttpServletRequest request=ServletActionContext.getRequest();  
        HttpSession session=request.getSession();
        
        //清空購物車
        session.removeAttribute("Cart");
        
        return result;        
    }
    
    public String Update() throws Exception {        
        
        String result="success";
        
        HttpServletRequest request=ServletActionContext.getRequest();  
        HttpSession session=request.getSession();
        
        HashSet<bookings.CartItem> cart=null;       
        cart=(HashSet<bookings.CartItem>)session.getAttribute("Cart");
        
//        for(bookings.CartItem ci : cart)        
//            ci.setQuantity(Integer.parseInt(request.getParameter("qq"+ci.getId())));
                
        //儲存回用戶的Session
        session.setAttribute("Cart", cart);
        
        return result;        
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getCampDate() {
        return campDate;
    }

    public void setCampDate(String campDate) {
        this.campDate = campDate;
    }
    
}
